import time

import allure
import pytest
from selenium.webdriver.common.by import By

from src.main.page.base_page import base_page
from src.main.utils import Log_ou


class Add_member_page(base_page):

    @allure.step("成员添加页面检验非法输入姓名的提示信息")
    def check_empty_input(self,name=None,phone=None,account=None,email=None,other_name=None,local_phone=None,address=None):

        _username_input_locate = (By.XPATH, "//*[@id='username']")
        user_input = self.find_ele_is_presence(_username_input_locate)
        _account_iniput_locate = (By.XPATH, "//*[@id='memberAdd_acctid']")
        account_input = self.find_ele_is_presence(_account_iniput_locate)
        _phone_input_locate = (By.XPATH, "//*[@class='qui_inputText ww_inputText ww_telInput_mainNumber']")
        phone_input = self.find_ele_is_presence(_phone_input_locate)
        _email_input_locate = (By.XPATH,"//*[@id='memberAdd_mail']")
        email_input = self.find_ele_is_presence(_email_input_locate)
        #别名输入框定位
        _other_name_input_locate = (By.XPATH,"//*[@id='memberAdd_english_name']")
        other_name_input = self.find_ele_is_presence(_other_name_input_locate)
        #座机输入框定位
        local_phone_input_locate= (By.XPATH,"//*[@id='memberAdd_telephone']")
        local_phone_input = self.find_ele_is_presence(local_phone_input_locate)
        #地址输入框定位
        address_input_locate = (By.XPATH,"//*[@id='memberEdit_address']")
        address_input = self.find_ele_is_presence(address_input_locate)

        if name !=None:
            if name =='n':
                user_input.send_keys(self.get_faker_name())
            else:
                user_input.send_keys(name)
        else:
            user_input.click()

        if account != None:
            if account == 'n':
                account_input.send_keys(self.get_faker_account())
            else:
                account_input.send_keys(account)
        else:
            account_input.click()

        if phone != None:
            if account == 'n':
                phone_input.send_keys(self.get_faker_number())
            else:
                phone_input.send_keys(phone)
        else:
            phone_input.click()

        if other_name != None:
            other_name_input.send_keys(other_name)
        if local_phone != None:
            local_phone_input.send_keys(local_phone)
        if address != None:
            address_input.send_keys(address)




        email_input.click()
        # 姓名提示标签
        _check_username_locate = (By.XPATH, "//*[preceding-sibling::input[@placeholder='姓名']]")
        # 账号提示标签
        _check_account_locate = (By.XPATH, "//*[preceding-sibling::input[contains(@placeholder,'成员唯一')]]")
        # 手机号提示标签
        _check_phone_locate = (By.XPATH, "//*[preceding-sibling::*[@class='ww_telInput']]")
        # 邮箱提示标签
        _check_email_locate = (By.XPATH, "//*[preceding-sibling::input[@id='memberAdd_mail']]")
        #别名提示标签
        _check_other_name_locate = (By.XPATH,"//*[preceding-sibling::input[@placeholder='别名']]")
        #座机提示标签
        _check_local_phone_locate = (By.XPATH,"//*[preceding-sibling::input[@id='memberAdd_telephone']]")
        #地址提示标签
        _check_address_locate = (By.XPATH,"//*[preceding-sibling::input[@id='memberEdit_address']]")

        user_name_warm = self.find_ele_is_presence(_check_username_locate)

        Log_ou.debug(f'用户名输入框异常:{user_name_warm.text}')
        print(f'用户名输入框异常:{user_name_warm.text}')
        u=user_name_warm.text

        account_warm = self.find_ele_is_presence(_check_account_locate)
        Log_ou.debug(f'账号输入框异常:{account_warm.text}')
        print(f'账号输入框异常:{account_warm.text}')
        a=account_warm.text

        phone_warm = self.find_ele_is_presence(_check_phone_locate)
        Log_ou.debug(f'手机输入框异常:{phone_warm.text}')
        print(f'手机输入框异常:{phone_warm.text}')
        p=phone_warm.text

        if email !=None:
            if account == 'n':
                phone_input.clear()
                email_input.send_keys(self.get_faker_email())
            else:
                phone_input.clear()
                email_input.send_keys(email)
        else:
            email_input.send_keys(1)

        self.driver.execute_script("arguments[0].blur();", email_input)

        time.sleep(1)

        email_warm = self.find_ele_is_presence(_check_email_locate)
        print(f'邮箱输入框异常:{email_warm.text}')
        Log_ou.debug(f'邮箱输入框异常:{email_warm.text}')
        e=email_warm.text

        other_name_warm = self.find_ele_is_presence(_check_other_name_locate)
        Log_ou.debug(f'别名输入框异常:{other_name_warm.text}')
        print(f'别名输入框异常:{other_name_warm.text}')
        o=other_name_warm.text
        local_phone_warm = self.find_ele_is_presence(_check_local_phone_locate)
        Log_ou.debug(f'座机输入框异常:{local_phone_warm.text}')
        print(f'座机输入框异常:{local_phone_warm.text}')
        l=local_phone_warm.text
        address_warm = self.find_ele_is_presence(_check_address_locate)
        Log_ou.debug(f'地址输入框异常:{address_warm.text}')
        print(f'地址输入框异常:{address_warm.text}')
        Add=address_warm.text

        return u,a,p,e,o,l,Add









    def add_member(self,phone=None,name=None,account=None):
        Log_ou.info('添加成员')
        _username_input_locate=(By.XPATH,"//*[@id='username']")
        user_input=self.find_ele_is_presence(_username_input_locate)
        _account_iniput_locate=(By.XPATH,"//*[@id='memberAdd_acctid']")
        account_input = self.find_ele_is_presence(_account_iniput_locate)
        # _email_input_locate=(By.XPATH,"//*[@id='memberAdd_mail']")
        # email_input = self.find_ele_is_presence(_email_input_locate)
        _phone_input_locate = (By.XPATH,"//*[@class='qui_inputText ww_inputText ww_telInput_mainNumber']")
        phone_input = self.find_ele_is_presence(_phone_input_locate)
        _save_btn_locate = (By.XPATH,"//*[@class='qui_btn ww_btn js_btn_save']")
        _check_to_invite_locate = (By.XPATH,"//span[contains(text(),'邀请通知')]/../input")
        self.find_ele_is_presence(_check_to_invite_locate).click()
        save_btn = self.find_ele_is_presence(_save_btn_locate)

        phone = phone if phone else self.get_faker_number()
        name = name if name else self.get_faker_name()
        account = account if account else self.get_faker_account()
        # phone = self.get_faker_number()
        # name = self.get_faker_name()
        # account= self.get_faker_account()

        user_input.send_keys(name)
        account_input.send_keys(account)
        phone_input.send_keys(phone)

        self.screen_shot()
        self.download_page_source()

        save_btn.click()
        success_create_department_locate = (By.XPATH, "//*[@class='ww_tip success']")
        time.sleep(1)
        success_tips = self.find_ele_is_presence(success_create_department_locate)
        return name,phone,success_tips.text


    def check_input_is_empty(self):
        Log_ou.info('检查必需字段为空时的情况')
        time.sleep(2)
        _username_input_locate = (By.XPATH, "//*[@id='username']")
        user_input = self.find_ele_is_presence(_username_input_locate)
        user_input.click()

        _account_iniput_locate = (By.XPATH, "//*[@id='memberAdd_acctid']")
        account_input = self.find_ele_is_presence(_account_iniput_locate)
        account_input.click()

        _phone_input_locate = (By.XPATH, "//*[@class='qui_inputText ww_inputText ww_telInput_mainNumber']")
        phone_input = self.find_ele_is_presence(_phone_input_locate)
        phone_input.click()

        _check_to_invite_locate = (By.XPATH, "//span[contains(text(),'邀请通知')]/../input")
        self.find_ele_is_presence(_check_to_invite_locate).click()

        _name_empty_warm_locate=(By.XPATH,"//*[@class='ww_compatibleTxt ww_compatibleTxt_Small ww_inputWithTips_WithErr']/div")
        name_warm_text = self.find_ele_is_presence(_name_empty_warm_locate).text
        _account_empty_warm_locate=(By.XPATH,"//*[@class='member_edit_item_right ww_inputWithTips_WithErr']/div")
        account_warm_text = self.find_ele_is_presence(_account_empty_warm_locate).text
        _phton_empty_warm_locate=(By.XPATH,"//label[text()='手机：']/..//*[@class='ww_inputWithTips_tips']")
        phone_warm_text = self.find_ele_is_presence(_phton_empty_warm_locate).text

        if(name_warm_text!=''):
            Log_ou.debug('用户名不能为空')
        if (account_warm_text != ''):
            Log_ou.debug('账号不能为空')
        if (phone_warm_text != ''):
            Log_ou.debug('电话不能为空')

        # time.sleep(20)

        return '请填写姓名','请填写账号','手机盒邮箱不能同时为空'





